package com.bioscript.standalone.imps;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class XLSfileWriter {
	
	List<String> columNames = null; 
	List<String> rowNames = null; 
	List<String> rows = null;
	String fileName = null;
	
	HSSFCellStyle cs1 = null;
	HSSFCellStyle cs2 = null;
	HSSFCellStyle cs3 = null;
	
	HSSFWorkbook wb = null;
	
	int index = 0;

	public XLSfileWriter(String fileName) {
		this.fileName = fileName;
	}
	
	public XLSfileWriter addColumnName(String columnName)
	{
		if (columNames == null)
		{
			columNames = new ArrayList<String>();
		}
		columNames.add(columnName);
		return this;
	}
	
	public XLSfileWriter addColumnNames(List<String> columns)
	{
		if (columNames == null)
		{
			columNames = new ArrayList<String>();
		}
		columNames.addAll(columns);
		return this;
	}
	
	public XLSfileWriter addRowName(String row)
	{
		if (rowNames == null)
		{
			rowNames = new ArrayList<String>();
		}
		rowNames.add(row);
		return this;
	}
	
	public XLSfileWriter addRowNames(List<String> row)
	{
		if (rowNames == null)
		{
			rowNames = new ArrayList<String>();
		}
		rowNames.addAll(row);
		return this;
	}
	
	public XLSfileWriter addRowData(String row)
	{
		if (rows == null)
		{
			rows = new ArrayList<String>();
		}
		rows.add(row);
		return this;
	}
	
	public XLSfileWriter addRowsData(List<String> row)
	{
		if (rows == null)
		{
			rows = new ArrayList<String>();
		}
		rows.addAll(row);
		return this;
	}
	
	public boolean write()
	{
		try {
			FileOutputStream fs = new FileOutputStream(fileName);
		    wb = new HSSFWorkbook();
		    HSSFSheet sheet = wb.createSheet();
		    HSSFRow row = sheet.createRow(0);
		    HSSFCell cell;
		    
		    cs1 = wb.createCellStyle( );
		    HSSFFont f1 = wb.createFont( );
		    f1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		    f1.setFontHeightInPoints((short)10);
		    f1.setFontName("Verdana");
		    cs1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		    cs1.setFont(f1);
		    
		    
		    cs2 = wb.createCellStyle( );
		    HSSFFont f2 = wb.createFont( );
		    f2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		    f2.setFontHeightInPoints((short)10);
		    f2.setColor(HSSFColor.RED.index);
		    f2.setFontName("Wingdings");
		    cs2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		    cs2.setFont(f2);
		    
		    
		    cs3 = wb.createCellStyle( );
		    HSSFFont f3 = wb.createFont( );
		    f3.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		    f3.setFontHeightInPoints((short)10);
		    f3.setColor(HSSFColor.GREEN.index);
		    f3.setFontName("Wingdings");
		    cs3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		    cs3.setFont(f3);

		    if (rowNames != null)
		    {
		    	cell = row.createCell(index);
		    	cell.setCellStyle(cs1);
		    	cell.setCellValue(""); 
		    	index++;
		    }
		    
		    
		    for (String columnName : columNames)
		    {
		    	cell = row.createCell(index);
		    	cell.setCellStyle(cs1);
		    	cell.setCellValue(columnName); 
		    	index++;
		    }
		    
		    // start creating rows
		    index = 1;
		    for (String rowData : rows)
		    {
		    	addRowData(rowData, sheet);
		    }
		    wb.write(fs);
		    fs.flush();
		    fs.close();
		} 
		catch(Exception ioe) 
		{
		    ioe.printStackTrace();
		    return false;
		}
		return true;
	}

	public void addRowData(String rowData, HSSFSheet sheet) 
	{
		
	}
}